<template>
  <el-dialog
    :title="id ? '编辑项目' : '新增项目'"
    :visible.sync="addTaskShow"
    :destroy-on-close="true"
    :append-to-body="true"
    width="45%"
    @close="handleClose"
    class="edit-main"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="执行项目：" prop="projectId">
        <el-select
          style="width: 80%"
          v-model="formData.projectId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务名称：" prop="taskName">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.taskName"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmit">提 交</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { projectList } from "@/api/projectManagement/list";
import { addTask } from "@/api/taskManagement/taskList.js";
export default {
  name: "addTaskDialog",
  props: {
    addTaskDialogShow: {
      type: Boolean,
      default: false,
    },
    questionText: {
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formData: {
        projectId: "",
        taskName: "",
      },
      rules: {
        projectId: [
          { required: true, message: "内容不能为空", trigger: "change" },
        ],
        taskName: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
      addTaskShow: false,
      productList: [],
    };
  },

  watch: {
    addTaskDialogShow: {
      handler(val) {
        if (val) {
          this.addTaskShow = val;
          this.getProductList();
        }
      },
      immediate: true,
    },
    questionText: {
      handler(val) {
        if (val) {
          this.formData.questionText = val;
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 保存
    onSubmit() {
      this.$refs["formData"].validate(async (valid) => {
        if (valid) {
        const item =   this.productList.find(el=>el.id == this.formData.projectId)
          const params = {
            projectName: item.projectName,
            ...this.formData
          };

          const { code } = await addTask(params);
          if (code === 200) {
            this.handleClose();
          }
        }
      });
    },

    // 项目列表
    async getProductList() {
      const params = {
        status: "",
        projectName: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await projectList(params);
      if (code === 200) {
        this.productList = rows;
      }
    },

    handleClose() {
      this.addTaskShow = false;
      this.formData = {
        projectId: "",
        taskName: "",
      };
      this.$emit("onCloseAddTaskDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.edit-main {
  ::v-deep .el-textarea__inner {
    height: 150px;
  }
}
</style>
